define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'exchange/code/index' + location.search,
                    add_url: 'exchange/code/add',
                    del_url: 'exchange/code/del',
                    multi_url: 'exchange/code/multi',
                    import_url: 'exchange/code/import',
                    table: 'exchange_code',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'library.name', title: __('Library.name'), operate: 'LIKE'},
                        {field: 'code', title: __('Code'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('已激活'),"2":__('已使用'),"3":__('已过期'),"0":__('未激活')}, formatter: Table.api.formatter.normal},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'mobile', title: __('手机号')}, 
                        {field: 'user_id', title: __('User_id')}, 
                        {field: 'order_id', title: __('Order_id')},
                        {field: 'operate', title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                if(row.status != 0){
                                    $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                }else{
                                    $(table).data("operate-del", true);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        goods: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'exchange/code/goods/ids/' + Config.ids,
                    multi_url: 'exchange/code/multi',
                    import_url: 'exchange/code/import',
                    table: 'exchange_code',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'library.name', title: __('Library.name'), operate: 'LIKE'},
                        {field: 'codes', title: __('Code'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('未使用'),"2":__('已使用'),"3":__('已过期')}, formatter: Table.api.formatter.normal},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'mobile', title: __('手机号')}, 
                        {field: 'user_id', title: __('User_id')}, 
                        {field: 'order_id', title: __('Order_id')}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
